package com.xyz.graph.util;


import com.xyz.graph.common.CommonResult;
import com.xyz.graph.exception.BuiltinSearchException;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Property;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


public class PathUtil {
    public static List<ArrayList<HashMap<String,String>>> pathAnalyze(GraphTraversal<Vertex, Path> paths,
                                                                      GraphTraversalSource g,
                                                                      boolean allData){
        if(null == g){
            throw new BuiltinSearchException(CommonResult.ERROR,"--pathAnalyze:输入的图数据库连接为空");
        }
        ArrayList<ArrayList<HashMap<String,String>>> res= new ArrayList<>();
        try {
            int iterateTimes = 0;
            if(allData){
                iterateTimes = Integer.MIN_VALUE;
            }
            //todo 最多取1000条路径出来计算
            while (paths.hasNext() && iterateTimes < 1000) {
                iterateTimes++;
                Path path = paths.next();
                Iterator<Object> iter = path.iterator();
                ArrayList<HashMap<String, String>> onePath = new ArrayList<>();
                while (iter.hasNext()) {
                    HashMap<String, String> map = new HashMap<>();
                    Object content = iter.next();
                    if (content instanceof Vertex) {
                        Vertex v = (Vertex) content;
                        Object vid = v.id();
                        map.put("parentType", "entity");
                        map.put("vid", vid.toString());
                        map.put("type", v.label());
                        GraphTraversal<Vertex, Property<Object>> prop = (GraphTraversal<Vertex, Property<Object>>) g.V(vid).properties();
                        while (prop.hasNext()) {
                            Property p = prop.next();
                            map.put(p.key(), p.value().toString());
                        }
                    } else {
                        Edge e = (Edge) content;
                        Object eid = e.id();
                        map.put("parentType", "relation");
                        map.put("vid", eid.toString());
                        map.put("type", e.label());
                        GraphTraversal<Edge, Property<Object>> prop = (GraphTraversal<Edge, Property<Object>>) g.E(eid).properties();
                        while (prop.hasNext()) {
                            Property p = prop.next();
                            map.put(p.key(), p.value().toString());
                        }
                    }
                    onePath.add(map);
                }
                res.add(onePath);
            }
        }catch(Exception e ){
            throw new BuiltinSearchException(CommonResult.ERROR,"--pathAnalyze:路径解析失败 " + e.getMessage());
        }
        return res;
    }
}

